<?php
use App\Exceptions\AppException;

if (!function_exists('app_response')) {
    /**
     * Undocumented function
     *
     * @param int $code
     * @param array $data
     * @param string $message
     * @param array $vars
     * @return \Illuminate\Http\JsonResponse
     * @author zhouyao <994914376@qq.com>
     */
    function app_response($code = 200, $data = [], $message = '', $vars = []) {
        if (!is_object($data)) {
            if (is_string($data)) {
                $message = $data;
            } elseif (empty($message)) {
                $message = config(sprintf('sys_msgs._MsgList.%s', $code), config('sys_msgs._MsgList.9999'));
            }

            // 替换message中可能存在的变量
            if (!empty($vars)) {
                array_unshift($vars, $message);
                $message = call_user_func_array('sprintf', $vars);
            }
        }

       return response()->json([
           'ret_code' => $code,
           'ret_msg' => $message,
           'data' => is_null($data) ? [] : $data
       ]);
    }
}

if (!function_exists('app_fail')) {
    /**
     *
     * User: zhouyao
     * Date: 2018/8/26
     * Time: 下午4:05
     * @param int $code
     * @param string $message
     * @param array $vars
     * @throws AppException
     */
    function app_fail($code = 9997, $message = '', $vars = []) {
        throw new AppException($code, $message, $vars);
    }
}

if (!function_exists('getCatGrandson')) {
    /**
     * 获取某个分类的 儿子 孙子 重子 重孙的 id
     * User: zhouyao
     * Date: 2018/8/27
     * Time: 上午9:46
     * @param $cate_id
     * @return array|mixed
     */
    function getCatGrandson($cate_id) {
        $GLOBALS['catGrandson'] = [];
        $GLOBALS['category_id_arr'] = [];
        // 先把自己的 id 保存起来
        $GLOBALS['catGrandson'][] = $cate_id;
        // 把整张表找出来
        $GLOBALS['category_id_arr'] = app('db')->table('goods_category')->pluck('parent_id', 'id');
        // 先把儿子找出来
        $son_id_arr = app('db')->table('goods_category')->where(['parent_id' => $cate_id])->pluck('id');
        collect($son_id_arr)->each(function ($v) {
            getCatGrandson2($v);
        });
        return $GLOBALS['catGrandson'];
    }
}

if (!function_exists('getCatGrandson2')) {
    /**
     * 递归调用找到重子重孙
     * User: zhouyao
     * Date: 2018/8/27
     * Time: 上午9:45
     * @param $cat_id
     */
    function getCatGrandson2($cat_id) {
        $GLOBALS['catGrandson'][] = $cat_id;
        foreach ($GLOBALS['category_id_arr'] as $k => $v) {
            // 找到孙子
            if ($v == $cat_id) {
                getCatGrandson2($k); // 继续找孙子
            }
        }
    }
}

if (!function_exists('get_arr_column')) {
    /**
     * 获取数组中的某一列
     * User: zhouyao
     * Date: 2018/8/27
     * Time: 上午10:26
     * @param $arr array     数组
     * @param $key_name string   列名
     * @return array             返回那一列的一维数组
     */
    function get_arr_column($arr = [], $key_name) {
        $tmp = array();
        foreach ($arr as $key => $val) {
            $tmp[] = $val[$key_name];
        }

        return $tmp;
    }
}